/*
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.examples.showcase.client.activities;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.examples.showcase.client.ClientFactory;
import com.googlecode.mgwt.examples.showcase.client.activities.animation.Animation;
import com.googlecode.mgwt.examples.showcase.client.activities.animation.Animation.AnimationNames;
import com.googlecode.mgwt.examples.showcase.client.activities.animation.AnimationSelectedEvent;
import com.googlecode.mgwt.examples.showcase.client.event.ActionEvent;
import com.googlecode.mgwt.examples.showcase.client.event.ActionNames;
import com.googlecode.mgwt.examples.showcase.client.rpc.AppAsyncCallback;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallDTO;
import com.googlecode.mgwt.mvp.client.MGWTAbstractActivity;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog.ConfirmCallback;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedHandler;

/**
 * @author Daniel Kurka
 * 
 */
public class UIActivity extends MGWTAbstractActivity {

	private final ClientFactory clientFactory;

	private int oldIndex;

	private List<Item> items;

	public UIActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;

	}

	@Override
	public void start(AcceptsOneWidget panel, final EventBus eventBus) {
		final UIView view = clientFactory.getUIView();

		view.setBackButtonText(clientFactory.getLabelResource().back());
		view.setTitle(clientFactory.getLabelResource().callsList());

		addHandlerRegistration(view.getBackButton().addTapHandler(new TapHandler() {

			@Override
			public void onTap(TapEvent event) {
				ActionEvent.fire(eventBus, ActionNames.BACK);

			}
		}));
		

		addHandlerRegistration(view.getList().addCellSelectedHandler(new CellSelectedHandler() {

			@Override
			public void onCellSelected(CellSelectedEvent event) {
				final int index = event.getIndex();

//				view.setSelectedIndex(oldIndex, false);
//				view.setSelectedIndex(index, true);
				oldIndex = index;
				items.get(index).getServiceCall();
				view.confirmCloseCall(items.get(index).getDisplayString(), 
						clientFactory.getLabelResource().closeCallConfirmation(), new ConfirmCallback() {

					@Override
					public void onOk() {
						closeCall(eventBus, items.get(index).getServiceCall());
					}

					private void closeCall(final EventBus aEventBus,ServiceCallDTO serviceCall) {
						final UIView view = clientFactory.getUIView();
						view.setBussy(true);
						clientFactory.getService().remoceCall(serviceCall.getKey(), new AppAsyncCallback<Void>() {

							@Override
							public void onInnerSuccess(Void aResult) {
								view.setBussy(false);
								AnimationSelectedEvent.fire(aEventBus, new Animation(AnimationNames.FLIP, "Flip"));
							}
							@Override
							public void onFailure(Throwable caught) {
								view.setBussy(false);
							}
						});
						
					}

					@Override
					public void onCancel() {

					}
				});
//				UIEntrySelectedEvent.fire(eventBus, items.get(index).getEntry());

			}
		}));

		panel.setWidget(view);
		loadCalls();
	}
	
	private void loadCalls(){
		clientFactory.getService().getAllCalls(new AppAsyncCallback<List<ServiceCallDTO>>() {

			@Override
			public void onInnerSuccess(List<ServiceCallDTO> aResult) {
				final UIView view = clientFactory.getUIView();
				items = createItems(aResult);
				view.renderItems(items);
				
			}
		});
	}

	/**
	 * @return
	 */
	private List<Item> createItems(List<ServiceCallDTO> callList) {
		ArrayList<Item> list = new ArrayList<Item>();
		for (ServiceCallDTO call : callList) {
			list.add(new Item(call));
		}
		
		return list;
	}

}
